package com.onenine.app.application;

import android.content.Context;
import android.os.Handler;
import android.support.multidex.MultiDex;

import com.faceunity.FURenderer;
import com.onenine.app.R;
import com.onenine.app.listener.NetStateChangeReceiver;
import com.onenine.app.listener.OnConnectStatusListener;
import com.onenine.app.listener.OnMessageReceiveListener;
import com.onenine.app.model.FriendAddMessage;
import com.onenine.app.model.GameTextMessage;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.base.BaseApp;
import com.onenine.chat.rongcloud.RcConnector;
import com.onenine.chat.rongcloud.RcImMgr;
import com.onenine.common.network.OkHttpUtils;
import com.onenine.common.network.interceptor.LoggerInterceptor;
import com.onenine.common.network.interceptor.RetryInterceptor;
import com.onenine.common.util.ProcessUtils;
import com.onenine.common.util.SPUtils;
import com.onenine.ddrawable.SuffixRegister;
import com.umeng.share.UMSharer;
import com.umeng.share.WxPayMgr;

import org.litepal.LitePal;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import io.rong.imlib.AnnotationNotFoundException;
import io.rong.imlib.RongIMClient;
import okhttp3.OkHttpClient;

public class NineteenApp extends BaseApp {

    public static final Handler sHandler = new Handler();

    @Override
    public void onCreate() {
        super.onCreate();
        if (Objects.equals(getPackageName()
                , ProcessUtils.getCurrentProcessName())) {
            initAppTools();
            initHttpConfigure();
            initRcConfigure();
            initThirdPlatformConfig();
            NetStateChangeReceiver.registerReceiver(this);
            RoleResLoader.registerRoleSuffixType();
            FURenderer.initFURenderer(this);
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    /**
     * 初始化工具
     */
    private void initAppTools() {
        SPUtils.setSpName(this.getPackageName());
        LitePal.initialize(this);
    }

    /**
     * 初始化http配置
     */
    private void initHttpConfigure() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .addInterceptor(new RetryInterceptor(1))
                .addInterceptor(new LoggerInterceptor("nineteen"))
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }

    /**
     * 初始化融云IM配置
     */
    private void initRcConfigure() {
        RcConnector.initialize(this);
        RcConnector.setConnectStatusListener(new OnConnectStatusListener());
        RcImMgr.setOnMessageReceiveListener(new OnMessageReceiveListener());
        try {
            RongIMClient.registerMessageType(FriendAddMessage.class);
            RongIMClient.registerMessageType(GameTextMessage.class);
        } catch (AnnotationNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化友盟登录分享配置
     */
    private void initThirdPlatformConfig() {
        UMSharer.initApp(this, getString(R.string.umeng_app_key));
        UMSharer.getInstance().setWeixinConfig(getString(R.string.weixin_app_key), getString(R.string.weixin_app_secret));
        UMSharer.getInstance().setQQConfig(getString(R.string.qq_app_key), getString(R.string.qq_app_secret));
        WxPayMgr.getInstance().registerWxPay(this, getString(R.string.weixin_app_key));
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        NetStateChangeReceiver.unregisterReceiver(this);
        SuffixRegister.getInstance().unregisterAll();
    }

}
